package com.zut.subject.domain.handler.subject;

import com.zut.subject.common.enums.IsDeletedFlagEnum;
import com.zut.subject.common.enums.SubjectTypeEnum;
import com.zut.subject.domain.convert.ConvertSubjectType;
import com.zut.subject.domain.entity.SubjectInfoBo;
import com.zut.subject.domain.entity.SubjectOptionAnswerBo;
import com.zut.subject.infra.basic.entity.SubjectJudge;
import com.zut.subject.infra.basic.entity.SubjectMultiple;
import com.zut.subject.infra.basic.entity.SubjectRadio;
import com.zut.subject.infra.basic.service.SubjectMultipleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class MultipleSubjectTypeHandlerImpl implements SubjectTypeHandler {


    @Resource
    private SubjectMultipleService subjectMultipleService;

    @Override
    public SubjectTypeEnum subjectEnum() {

        return SubjectTypeEnum.MULTIPLE;
    }

    @Override
    public void add(SubjectInfoBo subjectInfoBo) {
        //多选题逻辑
        List<SubjectOptionAnswerBo> subjectOptionAnswerBoList = subjectInfoBo.getOptionList();
        if (log.isInfoEnabled()){
            log.info("转化为选项List为：{}",subjectOptionAnswerBoList);
        }

        List<SubjectMultiple> subjectMultiple = ConvertSubjectType.INSTANCE.toListSubjectMultiple(subjectOptionAnswerBoList);
        if (log.isInfoEnabled()){
            log.info("传入多选数据库的数据为：{}",subjectMultiple);
        }
        //给多选题赋值题目id以及未被删除的状态
        subjectMultiple.forEach(optionList -> {
            optionList.setSubjectId(subjectInfoBo.getId());
            optionList.setIsDelete(IsDeletedFlagEnum.UN_DELETED.getCode());
        });
        subjectMultipleService.insertBatch(subjectMultiple);

    }

    @Override
    public SubjectInfoBo querySubjectOption(Integer subjectId) {

        List<SubjectMultiple> subjectMultipleList = subjectMultipleService.querySubjectOption(subjectId);
        List<SubjectOptionAnswerBo> Bo = ConvertSubjectType.INSTANCE.soaMultiple(subjectMultipleList);

        SubjectInfoBo subjectInfoBo =new SubjectInfoBo();
        subjectInfoBo.setOptionList(Bo);
        return subjectInfoBo;
    }
}
